package cn.itcast.service.impl;


import cn.itcast.dao.MemberDao;
import cn.itcast.dao.OrderDao;
import cn.itcast.pojo.Member;
import cn.itcast.service.ReportService;
import cn.itcast.utils.DateUtils;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private MemberDao memberDao;

    /**
     * 查询收入统计
     */
    @Override
    public List<Integer> income(List<String> months) {
        List<Integer> list = new ArrayList<>();
        for (String month : months) {
            Integer income = orderDao.findIncome(month);
            list.add(income);
        }
        return list;
    }

    /**
     * 每月预约数量统计
     */
    @Override
    public List<Integer> findByOrderNumber(List<String> months) {
        List<Integer> list = new ArrayList<>();
        for (String month : months) {
            Integer income = orderDao.findByOrderNumber(month);
            list.add(income);
        }
        return list;
    }

    /**
     * 每月支付数量统计
     */
    @Override
    public List<Integer> findByPayNumber(List<String> months) {
        List<Integer> list = new ArrayList<>();
        for (String month : months) {
            Integer income = orderDao.findByPayNumber(month);
            list.add(income);
        }
        return list;
    }

    /**
     * 运营数据统计
     * @return
     */
    @Override
    public Map<String, Object> getBusiness() throws Exception {

        //报表日期
        String todayDate = DateUtils.parseDate2String(DateUtils.getToday());
        //获取本周一的日期
        String monday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
        //获取本月第一天的日期
        String firstDay4ThisMonth = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());

        //今日新增会员数
        Integer todayNewMember = memberDao.findMemberCountByDate(todayDate);
        //总的会员数
        Integer totalMember = memberDao.findMemberTotal();
        //本周新增会员数
        Integer thisWeekNewMember = memberDao.findMemberCountByWeek(monday);
        //本月新增会员数
        Integer thisMonthNewMember = memberDao.findMemberCountAfterDate(firstDay4ThisMonth);

        //今日预约数
        Integer todayOrderNumber = orderDao.findOrderCountByDate(todayDate);
        //本周预约数
        Integer thisWeekOrderNumber =   orderDao.findOrderCountByWeek(monday);
        //本月预约数
        Integer thisMonthOrderNumber = orderDao.findOrderCountAfterDate(firstDay4ThisMonth);

        //今日到诊数
        Integer todayVisitsNumber = orderDao.findVisitsCountByDate(todayDate);
        //本周到诊数
        Integer thisWeekVisitsNumber = orderDao.findVisitsCountByWeek(monday);
        //本月到诊数
        Integer thisMonthVisitsNumber = orderDao.findVisitsCountAfterDate(firstDay4ThisMonth);

        Map<String,Object> map = new HashMap<>();
            map.put("reportDate", todayDate);//报表日期
            map.put("todayNewMember",todayNewMember);//今日新增会员数
            map.put("totalMember",totalMember);//总的会员数
            map.put("thisWeekNewMember",thisWeekNewMember);//本周新增会员数
            map.put("thisMonthNewMember",thisMonthNewMember);//本月新增会员数
            map.put("todayOrderNumber",todayOrderNumber);//今日预约数
            map.put("todayVisitsNumber",todayVisitsNumber);//今日到诊数
            map.put("thisWeekOrderNumber",thisWeekOrderNumber);//本周预约数
            map.put("thisWeekVisitsNumber",thisWeekVisitsNumber);//本周到诊数
            map.put("thisMonthOrderNumber",thisMonthOrderNumber);//本月预约数
            map.put("thisMonthVisitsNumber",thisMonthVisitsNumber);//本月到诊数
            //热门套餐，查询前4条
            List<Map> list = orderDao.findHotSetmeal();

            map.put("hotSetmeal",list);
            return map;
    }
}
